<template>
  <div id="login-page" class="style-4">
    <div class="login-title" v-text="'『'+companyName+'』后台登录'"></div>
    <div class="login-form">
      <el-form label-width="100px" style="width: 80%" :model="form" :rules="rules" ref="ruleForm">
        <el-form-item label="商户号:" prop="number">
          <el-input
            type="text"
            placeholder="请输入 商户号"
            v-model.trim="form.number"
            :maxlength="10"
            @keyup.enter.native="submit"></el-input>
        </el-form-item>
        <el-form-item label="帐号:" prop="username">
          <el-input
            type="text"
            placeholder="请输入 帐号"
            v-model.trim="form.username"
            :maxlength="30"
            @keyup.enter.native="submit"></el-input>
        </el-form-item>
        <el-form-item label="密码:" prop="password">
          <el-input
            type="password"
            placeholder="请输入 密码"
            v-model="form.password"
            @keyup.enter.native="submit"
            :maxlength="30"
            show-password></el-input>
        </el-form-item>
      </el-form>
      <div align="center">
        <el-button type="primary" style="width: 50%" @click="preSubmit" :loading="isLoading">提 交 登 录</el-button>
      </div>
    </div>
  </div>
</template>
<script>
import AesUtils from '../Tools/AesUtils'

export default {
  name: 'Login',
  mounted: function () {
    this.isLoading = false
    if (this.CurrUser.IsLogin()) {
      this.$router.push('/')
    }

    this.form.number = '02900006'
    this.form.username = 'admin'
    this.form.password = 'a123456'

  },
  data: function () {
    let $this=this
    var checkUserName1 = function (rule, value, callback) {
      if ($this.checkUserName(value)) {
        callback()
      } else {
        callback(new Error('账号 必须是有(A-Za-z0-9)长度在(2~30)位'))
      }
    }
    return {
      isLoading: false,
      form: {
        number: '',
        username: '',
        password: '',
      },
      rules: {
        number: [
          {required: true, message: '请输入商户号', trigger: 'blur'},
          {min: 5,max: 10, message: '长度在(3,10)个字符之间', trigger: 'blur'}
        ],
        username: [
          {required: true, message: '请输入帐号', trigger: 'blur'},
          {validator: checkUserName1, trigger: ['blur', 'change']},
        ],
        password: [
          {required: true, message: '请输入密码', trigger: 'blur'},
          {min: 6, message: '长度在 6 个字符以上', trigger: 'blur'}
        ],
      }
    }
  },
  methods: {
    preSubmit: function () {
      this.$refs.ruleForm.validate((valid) => {
        if (valid) {
          this.bus.$emit('MySliderCaptchaCallBus', 'Login', (sliderX) => {
            this.submit(sliderX)
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    // 提交
    submit: function (sliderX) {
      this.isLoading = true
      const form = {
        sliderX,
        number: this.form.number,
        username: this.form.username,
        password: this.form.password,
        from: 'ShopPC',
      }
      let data = JSON.stringify(form)
      data = AesUtils.Encrypt(data)
      this.MyRequest(this.ApiDoNameAuth + 'Public/r7wqyztzenb5k2k1', {data}, (data) => {
        data.username = this.form.username
        this.CurrUser.setCache(data)
        this.bus.$emit('UserInfoBusCall')
        location.reload()
      }, () => {
        this.$message.success('操作失败，请重试')
        this.isLoading = false
        this.$forceUpdate()
      })
    },
  }
}
</script>
